﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WINBASE.Controls
{
    public partial class WLCViewToolStrip : UserControl
    {
        public BindingList<KeyValuePair<string, string>> Column1, Column2;
        BindingList<string> ranges, comparisons, StatusCodes;
        bool isSystemMode = false;

        #region Constructor & Load

        public WLCViewToolStrip(WLCDataGridView datagrid)
        {
            InitializeComponent();
            this.DataGrid = datagrid;
            isSystemMode = true;
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            InitializeUI();
            InitializeEvent();
        }

        #endregion

        #region Event

        private void WLCViewToolStrip_Load(object sender, EventArgs e)
        {
            isSystemMode = false;
        }

        private void tspControl_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ToolStripItem ctrl = ((ToolStripItem)e.ClickedItem);
            switch (ctrl.Name)
            {
                case "btnClear1":
                    txtSearch1.Text = "";
                    break;
                case "btnClear2":
                    txtSearch2.Text = "";
                    break;
                case "btnRefresh":
                    this.dataGrid.RefreshData();
                    break;
                default:
                    break;
            }
        }

        void dataGrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.SelectedIndex = this.dataGrid.Columns[e.ColumnIndex].DisplayIndex;
        }

        private void btnChar1_Click(object sender, EventArgs e)
        {
            txtSearch1.Text = txtSearch1.Text.Trim() + "%";
        }

        private void btnChar2_Click(object sender, EventArgs e)
        {
            txtSearch2.Text = txtSearch2.Text.Trim() + "%";
        }

        private void txtSearch1_TextChanged(object sender, EventArgs e)
        {
            if (isSystemMode) return;
            this.dataGrid.Filter = this.QueryFilter;
            this.dataGrid.RefreshData();
        }

        private void txtSearch2_TextChanged(object sender, EventArgs e)
        {
            if (isSystemMode) return;
            this.dataGrid.Filter = this.QueryFilter;
            this.dataGrid.RefreshData();
        }

        private void cboComparison_SelectedChanged(object sender, EventArgs e)
        {
            if (isSystemMode) return;
            this.dataGrid.Filter = this.QueryFilter;
            this.dataGrid.RefreshData();
        }

        private void cboStatusCode_SelectedChanged(object sender, EventArgs e)
        {
            if (isSystemMode) return;
            this.dataGrid.Filter = this.QueryFilter;
            this.dataGrid.RefreshData();
        }

        #endregion

        #region Method

        public void InitializeData()
        {
            ranges = new BindingList<string> { "And","To" };
            comparisons = new BindingList<string> { "Is", "Not" };

            StatusCodes = new BindingList<string>(ApplicationService.QueryHelper.GetADataTable("SELECT 'ALL' AS StatusCode UNION ALL SELECT DISTINCT StatusCode FROM " + 
                                                    ApplicationObject.FrmModuleList.SelectedNode["MainTableName"] + " WHERE ISNULL(StatusCode,'') != ''")
                                                    .AsEnumerable()
                                                    .Select(row => row.Field<string>("StatusCode")).ToArray());

            cboComparison.DataSource = comparisons;
            cboRange.DataSource = ranges;
            cboStatusCode.DataSource = StatusCodes;

            InitColumn();
        }

        public void InitializeUI()
        {
            this.Dock = DockStyle.Fill;

            cboRange.SelectedIndex = cboRange.Items.Count > 0 ? 0 : -1;
            cboComparison.SelectedIndex = cboComparison.Items.Count > 0 ? 0 : -1;
            cboStatusCode.SelectedIndex = cboStatusCode.Items.Count > 0 ? 0 : -1;
        }

        public void InitializeEvent()
        {
            this.dataGrid.ColumnHeaderMouseClick += new DataGridViewCellMouseEventHandler(dataGrid_ColumnHeaderMouseClick);
        }

        public void InitColumn()
        {
            Column1 = new BindingList<KeyValuePair<string, string>>();
            Column2 = new BindingList<KeyValuePair<string, string>>();

            foreach (DataGridViewColumn dgvCol in this.dataGrid.DisplayColumns)
            {
                string key = dgvCol.DataPropertyName;
                string value = dgvCol.HeaderText;

                Column1.Add(new KeyValuePair<string, string>(key, value));
                Column2.Add(new KeyValuePair<string, string>(key, value));
            }

            cboColumn1.DataSource = Column1;
            cboColumn1.ValueMember = "Key";
            cboColumn1.DisplayMember = "Value";

            cboColumn2.DataSource = Column2;
            cboColumn2.ValueMember = "Key";
            cboColumn2.DisplayMember = "Value";

            this.SetComboIndex();
        }

        public void SetComboIndex()
        {
            base.Refresh();
            if (this.dataGrid.SortedColumn != null && this.dataGrid.SortedColumn.Visible == true)
                this.SelectedIndex = this.dataGrid.SortedColumn.DisplayIndex;
            else this.SelectedIndex = this.dataGrid.DisplayColumns.Count > 0 ? 0 : -1;
        }

        #endregion

        #region Property

        private WLCDataGridView dataGrid;
        public WLCDataGridView DataGrid
        {
            get { return dataGrid; }
            set { dataGrid = value; }
        }

        public string QueryFilter
        {
            get
            {
                string strQf = "";
                if (cboColumn1.SelectedIndex > -1 && txtSearch1.Text.Trim() != "")
                {
                    strQf += (strQf.Length <= 0 ? "" : " AND ") + '[' + cboColumn1.Text + ']' + " LIKE('" + txtSearch1.Text.Trim() + "%')";
                }
                if (cboColumn2.SelectedIndex > -1 && txtSearch2.Text.Trim() != "")
                {
                    strQf += (strQf.Length <= 0 ? "" : " AND ") + '[' + cboColumn2.Text + ']' + " LIKE('" + txtSearch2.Text.Trim() + "%')";
                }
                if (cboStatusCode.SelectedIndex > 0)
                {
                    strQf += (strQf.Length <= 0 ? "" : " AND ") + 
                             "[Status Code]" + 
                             (cboComparison.Text == "Is" ? " = " : " != ") + 
                             "'" + cboStatusCode.Text.Trim() + "'";
                }
                return strQf;
            }
        }

        private int _SelectedIndex = -1;
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set 
            {
                _SelectedIndex = value; 
                cboColumn1.SelectedIndex = value < cboColumn1.Items.Count ? value : -1;
                if (cboColumn2.Items.Count > 0) cboColumn2.SelectedIndex = _SelectedIndex == cboColumn2.Items.Count - 1 ? 0 : _SelectedIndex + 1;
                else cboColumn2.SelectedIndex = -1;
            }
        }

        #endregion
           }
}